package com.rykj.base.refreshview.impl;

import android.view.View;

import com.rykj.base.refreshview.data.IDataDelegate;
import com.rykj.base.refreshview.data.IDataSource;
import com.rykj.base.refreshview.view.IDataAdapter;
import com.rykj.base.refreshview.view.ILoadMoreListenerHandler;
import com.rykj.base.refreshview.view.IRefreshListLoadViewFactory;
import com.rykj.base.refreshview.view.IRefreshViewHolder;
import com.rykj.model.entity.PageInfoBase;
import com.rykj.model.entity.ResultBase;
import com.rykj.util.NoDoubleClicksListener;

import java.util.List;


/**
 * Description: author: zhangsan on 2017/8/11 下午3:20.
 */

public class DefaultModel2ViewControler<Data> implements IDataDelegate {

    private IDataSource dataSource;

    // private IRefreshListLoadViewFactory.IRefreshView refreshView;

    // private IRefreshListLoadViewFactory.ILoadMoreView loadMoreView;

    // private IRefreshListLoadViewFactory.IEmptyView  emptyView;
    // private IRefreshListLoadViewFactory iRefreshListLoadViewFactory;

    //private IDataAdapter dataAdapter;

    private IRefreshViewHolder refreshViewHolder;

    //private int loadAction=REFRESH;
    private boolean isRefreshWithEmptyView = false;

    private loadActionInterceptor loadActionInterceptor;
    private loadResultInterceptor loadResultInterceptor;
    private IDataSource.IDataSourceResultHandler<Data> taskResultHandler = new IDataSource.IDataSourceResultHandler<Data>() {
        @Override
        public void handleResult(int loadAction, Data data) {
            if (data == null) {
                handleError(new NullPointerException("数据异常"));
                return;
            }
            if (loadResultInterceptor != null) {
                loadResultInterceptor.onLoadResult(loadAction, data);
            }
            /**  判断是否拦截data后续处理**/
            if (loadResultInterceptor == null || (loadResultInterceptor != null && !loadResultInterceptor.shouldInterceptHanldle())) {
                switch (loadAction) {
                    case IDataSource.REFRESH:
                        if (isRefreshWithEmptyView) {
                            getEmptyView().loadFinish();
                        } else {
                            getRefreshView().stopRefresh();
                        }
                        if (data instanceof ResultBase) {
                            ResultBase resultBase = (ResultBase) data;
                            if (resultBase.data instanceof PageInfoBase) {
                                PageInfoBase result = (PageInfoBase) resultBase.data;
                                dataSource.setTotalPage(result.totalRows);
                                if (result.datas.isEmpty()) {
                                    if (refreshViewHolder.emptyViewEnable()) {
                                        getEmptyView().showEmpty("暂无数据");
                                    }
                                } else {
                                    getDataAdapter().refresh(result.datas);
                                }
                            } else if (resultBase.data instanceof List) {
                                List listData = (List) resultBase.data;
                                if (listData.isEmpty()) {
                                    if (refreshViewHolder.emptyViewEnable()) {
                                        getEmptyView().showEmpty("暂无数据");
                                    }
                                } else {
                                    getDataAdapter().refresh(listData);
                                }
                            } else if (resultBase.code != 200) {
                                if (refreshViewHolder.emptyViewEnable()) {
                                    getEmptyView().showEmpty(resultBase.message);
                                }
                            }
                        }
                        break;
                    case IDataSource.LOADMORE:
                        if (refreshViewHolder.loadMoreEnable()) {
                            if (dataSource.hasMore()) {
                                getLoadMoreView().hide();
                            } else {
                                getLoadMoreView().showNoMore("没有更多了");
                            }
                        }
                        if (data instanceof ResultBase) {
                            ResultBase resultBase = (ResultBase) data;
                            if (resultBase.data instanceof PageInfoBase) {
                                PageInfoBase datas = (PageInfoBase) resultBase.data;
                                dataSource.setTotalPage(datas.totalRows);
                                if (getDataAdapter() != null) {
                                    getDataAdapter().loadMore(datas.datas);
                                }
                            }
                        }
                        break;
                }
            }

        }

        @Override
        public void handleError(Throwable throwable) {
            if (refreshViewHolder.emptyViewEnable()) {
                getEmptyView().showLoadFail("加载失败");
            }

        }

        @Override
        public void handleErrorMsg(int code, String msg) {
            if (refreshViewHolder.emptyViewEnable()) {
                getEmptyView().showLoadFail(msg);
            }

        }
    };
    private IRefreshListLoadViewFactory.IRefreshView.onRefreshListener onRefreshListener = new IRefreshListLoadViewFactory.IRefreshView.onRefreshListener() {
        @Override
        public void onRefresh() {
            isRefreshWithEmptyView = false;
            // getRefreshView().startRefresh();
            if (loadActionInterceptor != null && loadActionInterceptor.shouldOverrideRefresh()) {
                loadActionInterceptor.onStartRefresh();
            }
            if (loadActionInterceptor == null || (loadActionInterceptor != null && !loadActionInterceptor
                    .shouldOverrideRefresh())) {
                refresh();
            }
        }
    };

    //  private boolean refreshViewEnable=true;

    // private boolean loadMoreViewEnable=false;

    //  private boolean emptyViewEnable=true;
    private ILoadMoreListenerHandler.ScrollBottomListener scrollBottomListener = new ILoadMoreListenerHandler.ScrollBottomListener() {
        @Override
        public void onScrollBottom() {
            if (dataSource.hasMore()) {
                dataSource.setCurrentPage(dataSource.getCurrentPage() + 1);
                getLoadMoreView().showLoading("");
                if (loadActionInterceptor != null) {
                    loadActionInterceptor.onStartLoadMore();
                }
                loadData(IDataSource.LOADMORE);
            } else {
                getLoadMoreView().showNoMore("没有更多了");
            }
        }
    };

    public DefaultModel2ViewControler(IRefreshViewHolder refreshViewHolder) {
        this.refreshViewHolder = refreshViewHolder;
        if (refreshViewHolder.loadMoreEnable()) {
            refreshViewHolder.getILoadMoreListenerHandler().setScrollBottomListener(
                    refreshViewHolder.getRefreshView().getContentView(),
                    scrollBottomListener);
        }
        refreshViewHolder.getRefreshView().setRefreshListener(onRefreshListener);
        if (refreshViewHolder.emptyViewEnable()) {
            getEmptyView().setReloadClickListener(new NoDoubleClicksListener() {
                @Override
                public void noDoubleClick(View v) {
                    refreshWithLoading();
                }
            });
        }
    }

    private IRefreshListLoadViewFactory.IEmptyView getEmptyView() {
        return refreshViewHolder.getEmptyView();
    }

    @Override
    public void refresh() {
        dataSource.setCurrentPage(1);
        loadData(IDataSource.REFRESH);

    }

    @Override
    public void refreshWithLoading() {
        if (!refreshViewHolder.emptyViewEnable() || getEmptyView() == null) {
            return;
        }
        isRefreshWithEmptyView = true;
        getEmptyView().showLoading("加载中...");
        refresh();
    }

    @Override
    public void destroy() {
        cancel();
        taskResultHandler = null;
    }

    public void cancel() {
        if (dataSource != null) {
            dataSource.cancel();
        }
    }

    @Override
    public void loadData(int loadAction) {
        dataSource.requestData(loadAction, taskResultHandler);
    }

    @Override
    public boolean isLoadWithEmptyView() {
        return isRefreshWithEmptyView;
    }

    @Override
    public void addLoadActionInterceptor(loadActionInterceptor interceptor) {
        this.loadActionInterceptor = interceptor;
    }

    @Override
    public void addLoadResultInterceptor(loadResultInterceptor interceptor) {
        this.loadResultInterceptor = interceptor;
    }

    @Override
    public DefaultModel2ViewControler setDataSource(IDataSource dataSource) {
        this.dataSource = dataSource;
        return this;
    }

    private IDataAdapter getDataAdapter() {
        return refreshViewHolder.getDataAdapter();
    }

    private IRefreshListLoadViewFactory.IRefreshView getRefreshView() {
        return refreshViewHolder.getRefreshView();
    }

    private IRefreshListLoadViewFactory.ILoadMoreView getLoadMoreView() {
        return refreshViewHolder.getLoadMoreView();
    }
}
